<?php


namespace App\Utils;


use App\Exception\BusinessException;
use Hyperf\Server\Exception\RuntimeException;
use Swlib\Http\ContentType;
use Swlib\Saber;

class IDCard
{
    public static function check($idcard, $name)
    {
        $appCode = config('aliidcard.appCode');


        $saber = Saber::create([
            'base_uri' => 'http://checkone.market.alicloudapi.com',
            'headers' => [
                'Authorization' => "APPCODE {$appCode}",
                'Content-Type' => ContentType::URLENCODE
            ]
        ]);
        $path = "/chinadatapay/1882" ;
        $query = "?idcard={$idcard}&name={$name}";
        try {
            $result = $saber->post($path . $query)->getParsedJsonArray();
            Log::get()->info(json_encode($result));
            if (isset($result['code']) && $result['code'] == 10000) {
                return $result;
            } else {
                throw new BusinessException(400, '查询无结果');
            }
        } catch (\Throwable $e) {
            Log::get()->error($e->getCode(), [$e->getMessage()]);
            Log::get()->error($e->getTraceAsString());
            //实名验证系统繁忙
            throw new RuntimeException($e->getMessage(), $e->getCode());
        }
    }

}